//index.js
import {IndexModel} from '../../model/index'
import {random} from '../../utils/randomStr'
const indexModel = new IndexModel()
Page({
  data: {
    articleList:[],
    markList:[],
    recommend:{},
    getMore:'',
    magazineId:0,
    loading:true
  },
  onLoad: function (options) {
    this.getData()
    // wx.showLoading()
  },
  onReachBottom(){
    this.setData({
      getMore:random(20)
    })
  },
  getData(magazineId){
    const articleList =indexModel.getArticleList(magazineId)
    const markList = indexModel.getMarkList(magazineId)
    const recommend = indexModel.getRecommendInfo(magazineId)
    Promise.all([articleList,markList,recommend]).then(res=>{
      console.log(res[0],res[1],res[2])
      this.setData({
        articleList:res[0],
        markList:res[1],
        recommend:res[2]
      })
      this.hideLoading()
    })
  },
  onCatalog(){
    wx.switchTab({
      url: '/pages/catalog/catalog'
    })
  },
  hideLoading(){
    this.setData({
      loading:false
    })
  },
  onNav(e){
    console.log(e)
    const magazineId = e.detail.index
    this.setMagazineId(magazineId)
    this.resetData()
    this.scrollPageToTop()
    this.getData(magazineId)
  },
  resetData(){
    this.setData({
      articleList:[],
      markList:[],
      recommend:{},
    })
  },
  scrollPageToTop(){
    wx.pageScrollTo({
      scrollTop:0,
      duration:0
    })
  },
  setMagazineId(magazineId) {
    this.setData({
      magazineId
    })
  }
})
